package com.arpa.wms.service;
import cn.hutool.core.collection.IterUtil;
import com.arpa.wms.domain.consts.CommonConst;
import com.arpa.wms.domain.dto.DeliveryDTO;
import com.arpa.wms.domain.dto.DeliveryDistributionDTO;
import com.arpa.wms.domain.dto.OutboundRegisterDTO;
import com.arpa.wms.domain.entity.DeliveryDistribution;
import com.arpa.wms.domain.entity.Outbound;
import com.arpa.wms.mapper.OutboundMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 复核出库确认Service
 */
@Service
@Log4j2
public class ReCheckService {

    @Autowired
    private IDeliveryService iDeliveryService;

    @Resource
    private OutboundMapper outboundMapper;

    @Autowired
    private OutCheckService outCheckService;
    /**
     * 异步批量复核出库确认
     * @author  rfwang
     * @date 2022/5/25 20:29
     * @param outboundCodes
     * @return com.arpa.core.web.Result
     */
    @Async
    public void batchRecheckOutboundConfirm(List<String> outboundCodes,String groupCode){
        if (IterUtil.isEmpty(outboundCodes)) {
            return;
        }
        try {
            //1.根据出库单号出库自动复核
            List<String> deliveryCodes = autoReview(outboundCodes, groupCode);
            //2.出库自动确认
            outCheckService.autoOutConfirmationByDeliveryCodes(deliveryCodes, groupCode);
            //3.释放容器
            outCheckService.releaseContainer(deliveryCodes,groupCode);
        } catch (Exception e) {
            log.error("批量复核出库错误信息为：{}",e.getMessage());
        }
        //更新出库单为任务完成
        outboundMapper.updateRecheckTaskStatus(outboundCodes, groupCode, CommonConst.STR_THREE);
    }

    /**
     * 根据出库单号出库自动复核
     *
     * @param outboundCodes 出库单号集合
     * @param groupCode 组织机构
     */
    @Transactional(rollbackFor = Exception.class)
    public List<String> autoReview(List<String> outboundCodes,String groupCode) {
        //批量更新订单及其明细复核数量
        List<String> deliveryCodes = outboundMapper.selectList(new LambdaQueryWrapper<Outbound>()
                .select(Outbound::getDeliveryCode)
                .in(Outbound::getCode, outboundCodes)
                .eq(Outbound::getGroupCode, groupCode)
        ).stream().map(Outbound::getDeliveryCode).collect(Collectors.toList());
        DeliveryDistributionDTO deliveryDistributionDTO = new DeliveryDistributionDTO();
        deliveryDistributionDTO.setDeliveryCodes(deliveryCodes);
        deliveryDistributionDTO.setGroupCode(groupCode);
        iDeliveryService.autoUpdateRecheckQuantityByCodes(deliveryDistributionDTO);
        //批量更新出库单及其明细复核数量
        OutboundRegisterDTO outboundRegisterDTO = new OutboundRegisterDTO();
        outboundRegisterDTO.setOutboundCodes(outboundCodes);
        outboundRegisterDTO.setGroupCode(groupCode);
        outboundMapper.autoUpdateRecheckQuantityByCodes(outboundRegisterDTO);
        return deliveryCodes;
    }
}
